<?php
namespace RH\Newspage\Domain\Repository;

/***************************************************************
 *  Copyright notice
 *
 *  (c) 2013 
 *  All rights reserved
 *
 *  This script is part of the TYPO3 project. The TYPO3 project is
 *  free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  The GNU General Public License can be found at
 *  http://www.gnu.org/copyleft/gpl.html.
 *
 *  This script is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  This copyright notice MUST APPEAR in all copies of the script!
 ***************************************************************/

/**
 *
 *
 * @package newspage
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License, version 3 or later
 *
 */
class PageRepository extends \TYPO3\CMS\Extbase\Persistence\Repository {
	
	protected $defaultOrderings = array(
		'sorting' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING,
		'starttime' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_DESCENDING,
		'crdate' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_DESCENDING,
	);
	
	protected $defaultQuerySettings = NULL;
	
	public function findAll() {
		$query = $this->createQuery();
		$storagePageIds = $query->getQuerySettings()->getStoragePageIds();
		// $query->getQuerySettings()->setRespectStoragePage(false);
		// \TYPO3\CMS\Core\Utility\GeneralUtility::devLog('storagePageIds', 'PageRepository', 1, array($storagePageIds));
		$constraints[] = $query->in('doktype', '1,4');
		$result = $query->matching($query->logicalAnd($constraints))->execute();
		return $result;
	}
	
	
	public function findByPid($pid) {
		$querySettings = $this->objectManager->create('Tx_Extbase_Persistence_Typo3QuerySettings');
		$querySettings->setRespectStoragePage(FALSE);
		$this->setDefaultQuerySettings($querySettings);
		$query = $this->createQuery();
		$query->matching($query->equals('pid', $pid));
		$pages = $query->execute();
		return $pages;
	}
	
	// public function findAll() {
		// $query = $this->createQuery();
		// $query->getQuerySettings()->setRespectStoragePage(false);
		// $ordering = array(
			// 'pid' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING,
			// 'sorting' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING,
		// );
		// $query->setOrderings($ordering);
		// $allPages = $query->execute();
		 
		// for ($i = 0; $i < count($allPages); $i++) {
			// $allPages[$i]->setSubpages(new \TYPO3\CMS\ExtBase\Persistence\ObjectStorage());
		// }
		
		// $storage = new \TYPO3\CMS\ExtBase\Persistence\ObjectStorage();
		// $this->findSubpages($storage, $allPages, 0);
		// return $storage;
	// }
	
	// private function findSubpages($storage, $allPages, $pid) {
		// foreach ($allPages as $pageKey => $page) {
			// if ($pid === $page->getPid()) {
				// $storage->attach($page);
				// $subpages_storage = $page->getSubpages();
				// $this->findSubpages($subpages_storage, $allPages, $page->getUid());
			// }
		// }
		// return $storage;
	// }	
}
?>